(function() {

  window.app = {};

  app.initialize = function(data) {
    app.school_id = data.si;
    return app.token = data.tk;
  };

}).call(this);

(function() {
  var $school_languages, confirm_add_language, hide_add_language, show_language, start_add_language, try_save_language;

  window.language = {};

  $school_languages = {};

  start_add_language = function() {
    return $('#add-language').modal();
  };

  confirm_add_language = function() {
    var la;
    console.log("Confirming add language");
    la = $('#language-select').val();
    $school_languages[la] = [];
    if ($('#101-checkbox').attr('checked')) $school_languages[la].push('101');
    if ($('#201-checkbox').attr('checked')) $school_languages[la].push('201');
    if ($('#301-checkbox').attr('checked')) $school_languages[la].push('301');
    return try_save_language(la, function() {
      return show_language(la, $school_languages[la]);
    });
  };

  show_language = function(la, cl) {
    var tbody;
    tbody = "";
    if (_.contains(cl, '101')) tbody += "<tr><td>101</td></tr>";
    if (_.contains(cl, '201')) {
      $school_languages[la].push('201');
      tbody += "<tr><td>201</td></tr>";
    }
    if (_.contains(cl, '301')) {
      $school_languages[la].push('301');
      tbody += "<tr><td>301</td></tr>";
    }
    if ($("#" + la + "-display").length > 0) $("" + la + "-display").remove();
    return $('#class-display').prepend("<table id=\"" + la + "-display\" class=\"table-bordered span2 textcenter\" style=\"height:300px\">\n  <thead><tr><th>" + la + "</th></tr></thead>\n  <tbody>\n    " + tbody + "\n    <tr><td><a class=\"btn btn-dark btn-small\" onclick=\"language.remove('" + la + "')\">Remove</a></td></tr>\n  </tbody>\n</table>");
  };

  try_save_language = function(la, callback) {
    return $.post('/school/languages/save', {
      i: app.school_id,
      tk: app.token,
      la: la,
      cl: $school_languages[la].join(',')
    }, function(data) {
      if (data.error) {
        $.meow({
          message: "The server doesn't like your values ... Refresh the page and try again, and if that doesn't work contact us."
        });
      } else {
        callback();
      }
      return hide_add_language();
    });
  };

  hide_add_language = function() {
    console.log("Hiding add language");
    return $('#add-language').modal('hide');
  };

  language.load = function(data) {};

  language.remove = function(la) {
    return $("#" + la + "-display").remove();
  };

  language.initialize = function() {
    $('#add-language-btn').click(start_add_language);
    $('#confirm-language-btn').click(confirm_add_language);
    return $('#cancel-language-btn').click(hide_add_language);
  };

}).call(this);

(function() {

  window.initialize = function(data) {
    common.initialize();
    app.initialize(data);
    language.initialize();
    return $('#school-name').text(data.sn);
  };

}).call(this);
